﻿// <copyright file="MainForm.Doxygen.cs" company="Horiba Medical">
//     Copyright (c) Horiba Medical. All rights reserved.
// </copyright>
// <summary>
//     Doxygen part of the main form.
// </summary>

namespace DoxyOutParser.Forms
{
	using System;
	using System.ComponentModel;
	using System.IO;
	using System.Windows.Forms;
	using DoxyOutParser.Launcher;
	using DoxyOutParser.Parser;

	/// <summary>
	/// Doxygen part of the MainForm class code.
	/// </summary>
	public partial class MainForm
	{
		#region Buttons

		/// <summary>
		/// Action taken when clicking on the generate button of the Doxygen toolstrip.
		/// </summary>
		/// <param name="sender">Event sender.</param>
		/// <param name="e">Event arguments.</param>
		private void DoxygenToolStripButtonGenerate_Click(object sender, EventArgs e)
		{
			string workingDirectory = this.browseBoxDoxygen.Text;

			try
			{
				// Generate the doxygen warning log file
				string doxygenBinary;

				if (this.radioButtonDoxCygwin.Checked)
				{
					doxygenBinary = this.textBoxDoxCygwinBin.Text;
				}
				else if (this.radioButtonDoxWin32.Checked)
				{
					doxygenBinary = this.textBoxDoxWin32Bin.Text;
				}
				else
				{
					doxygenBinary = "doxygen";
				}

				DoxygenLauncher.Launch("doc/doxyfile.uKernel", workingDirectory, doxygenBinary);
			}
			catch (FileNotFoundException)
			{
				InfoForm infoBox = new InfoForm("The doxyfile corresponding to your project could not be found, you should choose another directory.", "Warning");
				infoBox.Show();
				this.DoxygenToolStripButtonBrowse_Click(sender, e);
			}
			catch (Win32Exception)
			{
				InfoForm infoBox = new InfoForm("The directory you provided is not valid, please choose another one.", "Warning");
				infoBox.Show();
			}
		}

		/// <summary>
		/// Action taken when clicking on the parse button of the Doxygen toolstrip.
		/// </summary>
		/// <param name="sender">Event sender.</param>
		/// <param name="e">Event arguments.</param>
		private void DoxygenToolStripButtonParse_Click(object sender, EventArgs e)
		{
			string workingDirectory = this.browseBoxDoxygen.Text;

			try
			{
				// Parse the doxygen warning log file
				if (this.radioButtonDoxCygwin.Checked)
				{
					this.doxygenEntryList = new CygwinDoxygenOutputParser().Parse(workingDirectory + @"\doxygen.warning.log", this.filterBoxDoxygen.Text);
				}
				else if (this.radioButtonDoxWin32.Checked)
				{
					this.doxygenEntryList = new Win32DoxygenOutputParser().Parse(workingDirectory + @"\doxygen.warning.log", this.filterBoxDoxygen.Text);
				}

				this.doxygenEntryList.Sort();
				this.doxygenEntryDataGridView.DataSource = this.doxygenEntryList;

				// Automatically resize the visible rows.
				this.doxygenEntryDataGridView.AutoSizeRowsMode = DataGridViewAutoSizeRowsMode.AllCells;
			}
			catch (FileNotFoundException)
			{
				InfoForm infoBox = new InfoForm("The warning log could not be found, either change directory or generate documentation.", "Warning");
				infoBox.Show();
			}
		}

		/// <summary>
		/// Action taken when clicking on the browse button of the Doxygen toolstrip.
		/// </summary>
		/// <param name="sender">Event sender.</param>
		/// <param name="e">Event arguments.</param>
		private void DoxygenToolStripButtonBrowse_Click(object sender, EventArgs e)
		{
			FolderBrowserDialog chosenDoxyDir = new FolderBrowserDialog();
			chosenDoxyDir.RootFolder = System.Environment.SpecialFolder.MyComputer;
			chosenDoxyDir.Description = "Please select your 4HH uKernel project directory:";
			if (chosenDoxyDir.ShowDialog() == DialogResult.OK)
			{
				////this.Trace("The directory "+ chosenDoxyDir.SelectedPath +" has been selected");
			}

			try
			{
				this.browseBoxDoxygen.Text = chosenDoxyDir.SelectedPath;
			}
			catch (System.ArgumentException exception)
			{
				InfoForm infoBox = new InfoForm(exception.Message, "Exception occured");
				infoBox.Show();
			}
		}

		/// <summary>
		/// Action taken when clicking on the filter button of the Doxygen toolstrip.
		/// </summary>
		/// <param name="sender">Event sender.</param>
		/// <param name="e">Event arguments.</param>
		private void DoxygenToolStripButtonFilter_Click(object sender, EventArgs e)
		{
			string workingDirectory = this.browseBoxDoxygen.Text;

			// Parse the doxygen warning log file
			if (this.radioButtonDoxCygwin.Checked)
			{
				this.doxygenEntryList = new CygwinDoxygenOutputParser().Parse(workingDirectory + @"\doxygen.warning.log", this.filterBoxDoxygen.Text);
			}
			else if (this.radioButtonDoxWin32.Checked)
			{
				this.doxygenEntryList = new Win32DoxygenOutputParser().Parse(workingDirectory + @"\doxygen.warning.log", this.filterBoxDoxygen.Text);
			}

			this.doxygenEntryList.Sort();
			this.doxygenEntryDataGridView.DataSource = this.doxygenEntryList;

			// Automatically resize the visible rows.
			this.doxygenEntryDataGridView.AutoSizeRowsMode = DataGridViewAutoSizeRowsMode.AllCells;
		}

		#endregion

		#region TextBoxes

		/// <summary>
		/// Action taken when pressing a key on the filter textbox of the Doxygen toolstrip.
		/// </summary>
		/// <param name="sender">Event sender.</param>
		/// <param name="e">Event arguments.</param>
		private void DoxygenToolStripTextBoxFilter_KeyPress(object sender, KeyPressEventArgs e)
		{
			this.DoxygenToolStripButtonFilter_Click(sender, e);
		}

		/// <summary>
		/// Action taken when double clicking on the browse textbox of the Doxygen toolstrip.
		/// </summary>
		/// <param name="sender">Event sender.</param>
		/// <param name="e">Event arguments.</param>
		private void DoxygenToolStripTextBoxBrowse_DoubleClick(object sender, EventArgs e)
		{
			this.ButtonDoxyBinCygwinBrowse_Click(sender, e);
		}

		/// <summary>
		/// Action taken when changing the text on the browse textbox of the Doxygen toolstrip.
		/// </summary>
		/// <param name="sender">Event sender.</param>
		/// <param name="e">Event arguments.</param>
		private void DoxygenToolStripTextBoxBrowse_TextChanged(object sender, EventArgs e)
		{
			user.Default.uKernelDir = this.browseBoxDoxygen.Text;
			user.Default.Save();
		}

		#endregion

		#region EntryDataGrid

		/// <summary>
		/// Action taken when double clicking on the row header of the Doxygen entry data grid.
		/// </summary>
		/// <param name="sender">Event sender.</param>
		/// <param name="e">Event arguments.</param>
		private void DoxygenEntryDataGridView_RowHeaderMouseDoubleClick(object sender, DataGridViewCellMouseEventArgs e)
		{
			if (this.doxygenEntryDataGridView.SelectedRows.Count == 1)
			{
				OutputEntry entryInCell = (OutputEntry)this.doxygenEntryDataGridView.SelectedRows[0].DataBoundItem;
				try
				{
					if (this.radioButtonNotepadPP.Checked)
					{
						NotepadPPLauncher.Launch(entryInCell.FileNameAndPath, entryInCell.Line);
					}
					else if (this.radioButtonTextpad.Checked)
					{
						TextPadLauncher.Launch(entryInCell.FileNameAndPath, entryInCell.Line);
					}
					else if (this.radioButtonSourceInsight.Checked)
					{
						SourceInsightLauncher.Launch(entryInCell.FileNameAndPath, entryInCell.Line);
					}
				}
				catch (Win32Exception exception)
				{
					InfoForm infoBox = new InfoForm(exception.Message, "Exception occured");
					infoBox.Show();
				}
			}
			else
			{
				InfoForm infoBox = new InfoForm("There is nothing to open, try parsing a warning log first.", "Warning");
				infoBox.Show();
			}
		}

		/// <summary>
		/// Action taken when double clicking on the column header of the Doxygen entry data grid.
		/// </summary>
		/// <param name="sender">Event sender.</param>
		/// <param name="e">Event arguments.</param>
		private void DoxygenEntryDataGridView_ColumnHeaderMouseDoubleClick(object sender, DataGridViewCellMouseEventArgs e)
		{
			this.doxygenEntryList.Sort();
		}

		#endregion
	}
}
